﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LoginModule;
namespace HDVN5.Account
{
    public partial class DangKyThanhLapThanhCong : System.Web.UI.Page
    {
        private Int32 id;
        private LoginModule.DangKy inf = new LoginModule.DangKy();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                id = Convert.ToInt32(Request.QueryString["id"]);
                LoginModule.SOQUANLYDONVI thongTinDonVi1 = inf.thongTinDonVi(id);
                MaSoDonVi.Text = id.ToString();
                TenDonVi.Text = thongTinDonVi1.TENDONVIDANGKY;
                CapDonVi.Text = thongTinDonVi1.CAPID.ToString();
                HoVaTenTruong.Text = thongTinDonVi1.TENTRUONG;
                Email.Text = thongTinDonVi1.EMAIL;
                if (thongTinDonVi1.CAPID == 1)
                {
                    ThongTinChiTiet.DataSource = inf.thongTinDonViChiTiet((Int32)id);
                    ThongTinChiTiet.DataBind();
                }
            }
        }

        protected void btnDangKyTaiKhoan_Click(object sender, EventArgs e)
        {
            DANGKYSUDUNG suDung = new DANGKYSUDUNG();
            suDung.MASO = Convert.ToInt32(MaSoDonVi.Text);
            suDung.CAPDANGKY = Convert.ToInt32(CapDonVi.Text);
            suDung.TENTAIKHOAN = MaSoDonVi.Text;
            suDung.MATKHAU = LoginModule.Utilites.Encrypt(MaSoDonVi.Text, true,MaSoDonVi.Text);
            suDung.NGAYDANGKY = DateTime.Now.Date;
            suDung.NGAYTAO = DateTime.Now.Date;
            suDung.EMAIL = Email.Text;
            suDung.HOATDONG = true;
            int them = inf.dangKyTaiKhoan(suDung);
            if (them == 1)
            {
                if (CapDonVi.Text.Equals("1"))
                {
                    IList<LoginModule.SOQUANLYDONVI> lstChiTiet = inf.thongTinDonViChiTiet(Convert.ToInt32(MaSoDonVi.Text));
                    foreach (LoginModule.SOQUANLYDONVI item in lstChiTiet)
                    {
                        DANGKYSUDUNG dkySuDungCap4 = new DANGKYSUDUNG();
                        dkySuDungCap4.MASO = item.MASO;
                        dkySuDungCap4.CAPDANGKY = 4;
                        dkySuDungCap4.TENTAIKHOAN =item.MASO.ToString();
                        dkySuDungCap4.MATKHAU = LoginModule.Utilites.Encrypt(item.MASO.ToString(), true,item.MASO.ToString());
                        dkySuDungCap4.NGAYDANGKY = DateTime.Now.Date;
                        dkySuDungCap4.NGAYTAO = DateTime.Now.Date;
                        dkySuDungCap4.EMAIL = item.EMAIL ;
                        dkySuDungCap4.HOATDONG = true;
                        int d = inf.dangKyTaiKhoan(dkySuDungCap4);
                    }
                }

                Response.Write("<script>alert('Đăng ký tài khoản thành công')</script)");
                Response.Redirect("/Default.aspx");
            }
        }
    }
}